package com.debai.service.Impl;

import com.debai.mapper.ScreenMapper;
import com.debai.pojo.Screen;
import com.debai.pojo.User;
import com.debai.service.UserService;
import com.debai.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ScreenServiceImpl implements com.debai.service.ScreenService {
    @Autowired
    private ScreenMapper screenMapper;
    @Autowired
    private UserService userService;


    @Override
    public int insert(Screen screen, String token) {
        Map<String, Object> map = JwtUtil.parseToken(token);
        Integer userId = (Integer) map.get("userId");
        User user = userService.selectById(userId);
        screen.setPositionId(user.getPositionId());
        screen.setUserId(userId);

        int i = screenMapper.insert(screen);
        return i;
    }

    @Override
    public List<Screen> selectAll(String token) {
        Map<String, Object> map = JwtUtil.parseToken(token);
        int userId = (int) map.get("userId");
        User user = userService.selectById(userId);
        int positionId = user.getPositionId();
        List<Screen> screens = screenMapper.selectAll(positionId);
        return screens;
    }

    @Override
    public int update(Screen screen,String token) {
        Map<String, Object> map = JwtUtil.parseToken(token);
        Integer positionId = (Integer) map.get("positionId");
        System.out.println(positionId);
        screen.setPositionId(positionId);

        int i = screenMapper.update(screen);
        return i;
    }

    @Override
    public int delete(int id) {

        return screenMapper.delete(id);
    }

    @Override
    public Screen selectByName(String screenPosition) {
        Screen screen = screenMapper.selectByName(screenPosition);
        return screen ;
    }

    @Override
    public Screen selectById(int screenId) {
        Screen screen = screenMapper.selectById(screenId);
        return screen;
    }
}
